package config

import (
	"common/helper"
	"database/sql"
)

/*
有效邀请奖励配置
*/

type CfgProxyInviteBonusItem struct {
	ValidCounts int     `json:"valid_Counts" db:"valid_Counts" cbor:"valid_Counts"`
	Bonus       float64 `json:"bonus" db:"bonus" cbor:"bonus"`
	Id          int     `json:"id" db:"id" cbor:"id"`
}

type CsProxyInviteBonusCfg struct {
}

var cfgProxyInviteBonusList []CfgProxyInviteBonusItem

func (that *CsProxyInviteBonusCfg) GetCfg() []CfgProxyInviteBonusItem {
	return cfgProxyInviteBonusList
}

func ProxyInviteBonusCfgSetRow(rows *sql.Rows) {
	var tVal CfgProxyInviteBonusItem
	var err error
	err = rows.Scan(&tVal.ValidCounts, &tVal.Bonus, &tVal.Id)
	if err != nil {
		helper.InfoLog("ProxyInviteBonusCfgSetRow,scan err")
		return
	}

	cfgProxyInviteBonusList = append(cfgProxyInviteBonusList, tVal)
}

func ProxyInviteBonusCfgRemove() {
	cfgProxyInviteBonusList = []CfgProxyInviteBonusItem{}
}

/*
根据有效邀请人数获得代理奖励
*/
func GetProxyBonusMaxStage(ValidCount int) int {
	ArrayLen := len(cfgProxyInviteBonusList)
	if ArrayLen == 0 {
		return 0
	}
	var ProxyBonusStage int = 0
	for i := 0; i < ArrayLen; i++ {
		if ValidCount >= cfgProxyInviteBonusList[i].ValidCounts {
			ProxyBonusStage = cfgProxyInviteBonusList[i].Id
		} else {
			break
		}
	}

	return ProxyBonusStage
}

/*
根据有效邀请人数获得代理奖励
*/
func GetProxyInviteBonusByStage(Stage int) float64 {
	ArrayLen := len(cfgProxyInviteBonusList)
	if ArrayLen == 0 {
		return 0.0
	}
	for i := ArrayLen - 1; i >= 0; i-- {
		if Stage >= cfgProxyInviteBonusList[i].Id {
			return cfgProxyInviteBonusList[i].Bonus
		}
	}

	return 0.0
}

type CfgProxyInviteBonusByDepositAmountItem struct {
	Deposit int     `json:"deposit" db:"deposit" cbor:"deposit"` //充值金额
	Bonus   float64 `json:"bonus" db:"bonus" cbor:"bonus"`       //奖励
}

var cfgProxyInviteBonusByDepositAmountList []CfgProxyInviteBonusByDepositAmountItem

func ProxyInviteBonusByDepositAmountCfgSetRow(rows *sql.Rows) {
	var tVal CfgProxyInviteBonusByDepositAmountItem
	var err error
	err = rows.Scan(&tVal.Deposit, &tVal.Bonus)
	if err != nil {
		helper.InfoLog("ProxyInviteBonusByDepositAmountCfgSetRow,scan err")
		return
	}

	cfgProxyInviteBonusByDepositAmountList = append(cfgProxyInviteBonusByDepositAmountList, tVal)
}

func ProxyInviteBonusByDepositAmountCfgRemove() {
	cfgProxyInviteBonusByDepositAmountList = []CfgProxyInviteBonusByDepositAmountItem{}
}

/*
根据下级充值金额获得代理奖励
*/
func GetProxyInviteBonusByDepoistAmount(childDepositAmount float64) float64 {
	ArrayLen := len(cfgProxyInviteBonusByDepositAmountList)
	if ArrayLen == 0 {
		return 0.0
	}
	for i := ArrayLen - 1; i >= 0; i-- {
		if childDepositAmount >= float64(cfgProxyInviteBonusByDepositAmountList[i].Deposit) {
			return cfgProxyInviteBonusByDepositAmountList[i].Bonus
		}
	}

	return 0.0
}
func (that *CsProxyInviteBonusCfg) GetDepoistAmountCfg() []CfgProxyInviteBonusByDepositAmountItem {
	return cfgProxyInviteBonusByDepositAmountList
}
